package functions.ems_base_data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import com.beans.AdminUserBean;
import com.beans.CompanyBean;
import com.crc.mam.BaseCase;
import coms.Constants;
import frame.HttpHelper;
import functions.ems_accountant_job_pool.Pool;

public class MdOu extends BaseCase{
	 private org.slf4j.Logger logger = LoggerFactory.getLogger(MdOu.class);
	 String result;
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	ArrayList<CompanyBean> companies;
	CompanyBean myCompany;
	public CompanyBean getMyCompany() {
		return myCompany;
	}
	public void setMyCompany(CompanyBean myCompany) {
		this.myCompany = myCompany;
	}
	AdminUserBean loginUserBean;
	
	
	public MdOu(AdminUserBean loginUserBean) {
	  this.loginUserBean = loginUserBean;
    }
	
	public MdOu() {
      }
	/**
	 * 查询公司
	 * findCurrentMdOuUrl="/ems-base-data/mdOu/findCurrentMdOu"
	 */
	public ArrayList<CompanyBean> findCurrentMdOu() {
	  companies = null;
		logger.info("查询公司 ----------------- start ------------");
		String url = Constants.FIND_CURRENT_MD_OU_URL;
		try {
			result = HttpHelper.post("", url);
			logger.info("findCurrentMdOu --- " + result);
			status = HttpHelper.isOk;
			if(status) {
			try {
				JSONArray array = new JSONArray(result);
				companies = new ArrayList<CompanyBean>();
				for (int i = 0; i < array.length(); i++) {
					JSONObject json = (JSONObject) array.get(i);
					try {
						String companyCode = json.getString("companyCode");
						String companyName = json.getString("ouName");
						CompanyBean company = new CompanyBean(companyCode, companyName);
						companies.add(company);
					} catch (JSONException e) {
						//e.printStackTrace();
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
				remark = "JSON解析错误";
			}
			}else {
			  JSONObject array = new JSONObject(result);
			  remark = array.getString("error");
			}
		} catch (Exception e) {
			status = false;
			remark = "系统异常";
		}
		logger.info("查询公司 ----------------- end ------------");
		return companies;
	}
	
	public ArrayList<CompanyBean> findAllMdOuList(){
	  companies = null;
	  logger.info("查询公司 ----------------- start ------------");
      String url = Constants.FIND_ALL_MD_OU_LIST_URL;
      try {
          result = HttpHelper.post("{}", url);
          logger.info("findCurrentMdOu --- " + result);
          try {
              JSONArray array = new JSONArray(result);
              companies = new ArrayList<CompanyBean>();
              for (int i = 0; i < array.length(); i++) {
                  JSONObject json = (JSONObject) array.get(i);
                  try {
                      String companyCode = json.getString("companyCode");
                      String companyName = json.getString("ouName");
                      CompanyBean company = new CompanyBean(companyCode, companyName);
                      companies.add(company);
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          
          
      } catch (Exception e) {
          // TODO Auto-generated catch block
          //e.printStackTrace();
      }
      logger.info("查询公司 ----------------- end ------------");
	  return companies;
	  
	}
	
	public CompanyBean getCompanyByCompanyName(String companyName) {
	  myCompany = null;
	  companies =  findCurrentMdOu();
	  if(companies !=null) {
	    if(companies.size()>0) {
	        for(CompanyBean compayBean : companies) {
	          if(compayBean.getCompanyName().equals(companyName)) {
	            myCompany = compayBean;
	            break;
	          }
	        }
	      }
	  }
	 
    return myCompany;
	  
	}

}
